import requests
from bs4 import BeautifulSoup


def getHtml(url):
    try:
        re = requests.get(url, timeout=10)
        re.raise_for_status()
        re.encoding = 'utf-8'
        print('获取成功')
        return re.text
    except:
        print('获取失败')
        return ''


def getHtmlData(html):
    soup = BeautifulSoup(html, 'html.parser')
    trs = soup.find_all('tr')
    # print(trs)
    all_ls = []
    for tr in trs:

        ls = []
        for td in tr:
            if td.string != '\n':
                ls.append(td.string)
        all_ls.append(ls)
    return all_ls


def wroitedata(ls):
    with open(r'd:中国大学排名.csv', 'w') as f:
        for line in ls:
            f.write(','.join(line[0:4]) + '\n')


def main():
    url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
    html = getHtml(url)
    getHtmlData(html)
    wroitedata(getHtmlData(html))

main()
